package com.cn.jinl.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.jinl.api.IOrderOutInfoService;
import com.cn.jinl.domain.Order;
import com.cn.jinl.domain.OrderOutInfo;
import com.cn.jinl.domain.OrderRankingVo;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.JsonResponse;
import com.cn.jinl.utils.QueryForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/orderOut")
public class OrderOutInfoController {

    @Autowired
    private IOrderOutInfoService orderOutInfoService;

    @RequestMapping("/getOrderOutInfoList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<OrderOutInfo>> getOrderOutInfoList(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<OrderOutInfo>> response = new JsonResponse<>();
        List<OrderOutInfo> orderOutInfos = orderOutInfoService.getOrderOutInfoList(queryForm);
        response.setResult(orderOutInfos);
        response.setSuccess(true);
        return response;
    }


    @RequestMapping("/getOrderOutRelateList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Order>> getOrderOutRelateList(String id, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<Order>> response = new JsonResponse<>();
        List<Order> order = orderOutInfoService.getOrderOutRelateList(id);
        response.setResult(order);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/getOrderInfoByParam/{frontToken}")
    @ResponseBody
    public JsonResponse<List<Order>> getOrderInfoByParam(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<Order>> response = new JsonResponse<>();
        List<Order> order = orderOutInfoService.getOrderInfoByParam(queryForm);
        response.setResult(order);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/disOrderOutInfo/{frontToken}")
    @ResponseBody
    public JsonResponse<String> disOrderOutInfo(OrderOutInfo orderOutInfo, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = orderOutInfoService.disOrderOutInfo(orderOutInfo);
        return CommonUtil.getResponseData(response,result);
    }


    //app获取发货任务
    @RequestMapping("/api/getAPIOrderOutInfoList")
    @ResponseBody
    public JsonResponse<List<OrderOutInfo>> getAPIOrderOutInfoList(QueryForm queryForm) throws Exception{
        JsonResponse<List<OrderOutInfo>> response = new JsonResponse<>();
        List<OrderOutInfo> orderOutInfos = orderOutInfoService.getAPIOrderOutInfoList(queryForm);
        response.setResult(orderOutInfos);
        response.setSuccess(true);
        return response;
    }

    @RequestMapping("/api/getAPIOrderOutRelateList")
    @ResponseBody
    public JsonResponse<List<Order>> getOrderOutRelateList(String id) throws Exception{
        JsonResponse<List<Order>> response = new JsonResponse<>();
        List<Order> order = orderOutInfoService.getOrderOutRelateList(id);
        response.setResult(order);
        response.setSuccess(true);
        return response;
    }
    //获取提示信息
    @RequestMapping("/api/getOrderOutTip")
    @ResponseBody
    public JsonResponse<String> updateAndGetOrderOutTip(String userId) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String tip = orderOutInfoService.getOrderOutTip(userId);
        response.setResult(tip);
        response.setSuccess(true);
        return response;
    }

    //首页发货任务
    @RequestMapping("/api/getSendOrderOutCount")
    @ResponseBody
    public JsonResponse<Map<String,Integer>> getSendOrderOutCount() throws Exception{
        JsonResponse<Map<String,Integer>> response = new JsonResponse<>();
        Map<String,Integer> maps = orderOutInfoService.getSendOrderOutCount();
        response.setResult(maps);
        response.setSuccess(true);
        return response;
    }

    //发货详情
    @RequestMapping("/getShippingDetailsList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<OrderRankingVo>> getShippingDetailsList(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<List<OrderRankingVo>> response = new JsonResponse<>();
        List<OrderRankingVo> order  = orderOutInfoService.getShippingDetailsList(queryForm);
        response.setResult(order);
        response.setSuccess(true);
        return response;
    }

    // 发货详情下载
    @ResponseBody
    @RequestMapping("/exportShippingDetail")
    public void exportShippingDetail(@RequestParam(value = "queryJson",required = true) String queryJson, HttpServletResponse response) throws Exception{
        QueryForm queryForm = new QueryForm();
        JSONObject jsonObject = JSON.parseObject(queryJson);
        queryForm.setItemId(URLDecoder.decode(jsonObject.getString("itemId"), "UTF-8"));
        queryForm.setName(URLDecoder.decode(jsonObject.getString("name"), "UTF-8"));
        queryForm.setCustomer(URLDecoder.decode(jsonObject.getString("customer"), "UTF-8"));
        orderOutInfoService.exportShippingDetail(queryForm,response);
    }
}
